package com.smbms.controller;

import com.smbms.pojo.User;
import com.smbms.service.UserService;
import com.smbms.util.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Resource
    private UserService userService;

    /**
     * 登录-数据接口
     */
    @ResponseBody
    @RequestMapping(value = "/userLogin",method = RequestMethod.POST)
    public Result login(@RequestBody User user, HttpSession session) {
        User loginUser = userService.getByLogin(user);
        if(loginUser != null){
            //登录成功，存储用户信息到Session会话中
            session.setAttribute("LOGIN_USER", loginUser);
            return new Result(true, "登陆成功", loginUser);
        }else{
            return new Result(false, "账号或密码输入错误");
        }
    }

    /**
     * 退出-数据接口
     */
    @ResponseBody
    @RequestMapping("/logout")
    public Result logout(HttpSession session) {
        //使当前session会话失效
        session.invalidate();

        //响应数据
        return new Result(true, "退出成功");
    }
}
